<?php
/* -------------------------------------------------------------------------------------
* 	ID:						$Id: gv_redeem.php 173 2013-09-22 07:55:10Z phone.mueller@googlemail.com $
* 	Letzter Stand:			$Revision: 173 $
* 	zuletzt geaendert von:	$Author: siekiera $
* 	Datum:					$Date: 2013-09-22 07:55:10 +0000 (Sun, 22 Sep 2013) $
*
* 	SEO:mercari by Siekiera Media
* 	http://www.seo-mercari.de
*
* 	Copyright (c) since 2011 SEO:mercari
* --------------------------------------------------------------------------------------
* 	based on:
* 	(c) 2000-2001 The Exchange Project  (earlier name of osCommerce)
* 	(c) 2002-2003 osCommerce - www.oscommerce.com
* 	(c) 2003     nextcommerce - www.nextcommerce.org
* 	(c) 2005     xt:Commerce - www.xt-commerce.com
*
* 	Released under the GNU General Public License
* ----------------------------------------------------------------------------------- */

require ('includes/application_top.php');

if (ACTIVATE_GIFT_SYSTEM != 'true')
	redirect(FILENAME_DEFAULT);

if (!isset ($_SESSION['customer_id'])) 
	redirect(FILENAME_SHOPPING_CART);

$smarty = new Smarty;

require (DIR_FS_CATALOG.'templates/'.CURRENT_TEMPLATE.'/source/boxes.php');

require (DIR_WS_INCLUDES.'header.php');

if (isset ($_GET['gv_no'])) {
	$error = true;
	
	$coupon = $db->db_query("SELECT 
								c.coupon_id, 
								c.coupon_amount 
							FROM 
								".TABLE_COUPONS." c, 
								".TABLE_COUPON_EMAIL_TRACK." et 
							WHERE 
								coupon_code = ".$db->db_prepare($_GET['gv_no'])." 
							AND 
								c.coupon_id = et.coupon_id");
	
	if ($coupon->_numOfRows > 0) {
		$redeem_query = $db->db_query("	SELECT 
											coupon_id 
										FROM 
											".TABLE_COUPON_REDEEM_TRACK." 
										WHERE 
											coupon_id = '".$coupon->fields['coupon_id']."'");
		
		if ($redeem_query->_numOfRows == 0) {
			$_SESSION['gv_id'] = $coupon->fields['coupon_id'];
			$error = false;
		} else {
			$error = true;
		}
	}
} else {
	redirect(FILENAME_DEFAULT);
}

if ((!$error) && (isset ($_SESSION['customer_id']))) {
	$gv_query = $db->db_query("INSERT INTO
								".TABLE_COUPON_REDEEM_TRACK." (
									coupon_id, 
									customer_id, 
									redeem_date, 
									redeem_ip
								)VALUES(
									'".$coupon->fields['coupon_id']."', 
									'".$_SESSION['customer_id']."', 
									NOW(),
									'".$REMOTE_ADDR."')");
	
	$gv_update = $db->db_query("UPDATE 
									".TABLE_COUPONS." 
								SET 
									coupon_active = 'N' 
								WHERE 
									coupon_id = '".$coupon->fields['coupon_id']."'");
	
	gv_account_update($_SESSION['customer_id'], $_SESSION['gv_id']);
	unset ($_SESSION['gv_id']);
}

$breadcrumb->add(NAVBAR_GV_REDEEM);

$smarty->assign('coupon_amount', $price->format($coupon->fields['coupon_amount'], true));
$smarty->assign('error', $error);
$smarty->assign('LINK_DEFAULT', '<a href="'.href_link(FILENAME_DEFAULT).'">'.image_button('button_continue.gif', IMAGE_BUTTON_CONTINUE).'</a>');
$smarty->assign('language', $_SESSION['language']);
$smarty->caching = false;
$main_content = $smarty->fetch(CURRENT_TEMPLATE.'/module/gv_redeem.html');

$smarty->assign('language', $_SESSION['language']);
$smarty->assign('main_content', $main_content);
$smarty->caching = false;
if (!defined('RM'))
$smarty->loadFilter('output', 'note');
$smarty->loadFilter('output','trimwhitespace');
$smarty->display(CURRENT_TEMPLATE.'/index.html');
include ('includes/application_bottom.php');
?>